package cn.zebra.dev.product.module.category.service.impl;

import cn.zebra.dev.common.utils.DateUtils;
import cn.zebra.dev.ibatis.plus.service.impl.ServiceImpl;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.ibatis.plus.utils.Query;
import cn.zebra.dev.product.module.category.dao.CategoryDao;
import cn.zebra.dev.product.module.category.entity.CategoryEntity;
import cn.zebra.dev.product.module.category.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author runnable@sina.cn
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils<CategoryEntity> findPage(Map<String, Object> params) {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id, parent_id, category_name, category_image, sort, show_flag");
        wrapper.apply(params.containsKey("categoryType"), "category_type = {0}", params.get("categoryType"));
        wrapper.apply(params.containsKey("parentId"), "parent_id = {0}", params.get("parentId"));
        wrapper.apply(params.containsKey("categoryId"), "id = {0}", params.get("categoryId"));
        wrapper.apply(params.containsKey("categoryName"), "category_name LIKE CONCAT('%', {0}, '%')", params.get("categoryName"));
        wrapper.apply("delete_flag = FALSE");
        wrapper.orderByDesc("id");
        IPage<CategoryEntity> iPage = this.page(
                new Query<CategoryEntity>(params).getPage(),
                wrapper
        );
        return new PageUtils<>(iPage);
    }

    @Override
    public List<CategoryEntity> findALLByIds(List<Long> ids) {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id, category_name ");
        wrapper.in("id", ids);
        return list(wrapper);
    }

    @Override
    public List<CategoryEntity> findByLevelType(Integer levelType) {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.apply("level_type = {0}", levelType);
        return list(wrapper);
    }

    @Override
    public List<CategoryEntity> findIdByLevel(Integer levelType) {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.apply("level_type = {0}",levelType);
        return list(wrapper);
    }

    @Override
    public List<CategoryEntity> findAllByParentIds(List<Long> parentIds) {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.in("parent_id", parentIds);
        return list(wrapper);
    }


    @Override
    public Boolean saveCategory(CategoryEntity entity, Long createId) {
        isParent(entity);
        entity.setCreateId(createId);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateId(0L);
        entity.setUpdateTime(DateUtils.defaultTime());
        entity.setDeleteFlag(false);
        return save(entity);
    }

    @Override
    public Boolean updateCategory(Long id, CategoryEntity entity, Long updateId) {
        isParent(entity);
        entity.setCreateId(null);
        entity.setCreateTime(null);
        entity.setUpdateId(updateId);
        entity.setUpdateTime(LocalDateTime.now());
        entity.setDeleteFlag(false);
        return update(entity, "id = {0}", id);
    }

    private void isParent(CategoryEntity entity) {
        Long parentId = entity.getParentId();
        if (parentId != null && parentId != 0L) {
            if (!isEntity(parentId)) {
                throw new RuntimeException("上级分类不存在!");
            }
        }
    }

    @Override
    public Boolean isEntity(Long id) {
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.apply("id = {0} and delete_flag = FALSE", id);
        return count(wrapper) > 0;
    }

}
